#!/bin/bash

source ./func_module.eclass

# TARGET_FILE="target-mso.txt"
TARGET_FILE=$1
SCHEDULE_DIR="/metadata/Redbee"
LINEUP_DIR="/metadata/Redbee/lineups-staging"
# SCHEDULE_DATE="20130903"
SCHEDULE_DATE=$2

function main {
  #FILENAME_PATTERN=20130725
  local TARGET_FILE=$1
  local SCHEDULE_DATE=$2
  local SCHEDULE_FILE_XML=""
  #SCHEDULE_FILE="/metadata/Redbee/be/20130903190005samsung.xml"
  local SCHEDULE_FILE_ONLY=""
  local SCHEDULE_FILE=""

  local LINEUP_FILE_XML=""
  #LINEUP_FILE_XML="BE_Telenet_TVA_Lineups_20130530_v8.xml"
  local LINEUP_FILE_ONLY=""
  local LINEUP_FILE=""
  local COUNTRY=""
  local MSO_FILE_PTN=""
  local HEADEND_ID=""
  local DEVICE_TYPE_CODE=""
  local HEADEND_NAME=""

  local NUM_CH_IN_HEADEND=""
  local NUM_CH_IN_HEADEND_WITH_SCHEDULE=""
  local NUM_CH_IN_SCHEDULE=""
  local LINE=""

  local OUTPUTDIR="STAT-${SCHEDULE_DATE}"
  if [ ! -d "$OUTPUTDIR" ]; then
    mkdir $OUTPUTDIR
  fi

  echo "||COUNTRY||NUM CH(S)||CH LANG STAT||MSO ID||MSO NAME||HEADEND ID||DEV CODE||HEADEND NAME||NUM CH||NUM CH(S)||CH LANG STAT||"

  for co_mso_line in `cat ${TARGET_FILE}`
  do
    COUNTRY=`echo $co_mso_line | awk -F "|" '{print $1}'`
    MSO_FILE_PTN=`echo $co_mso_line | awk -F "|" '{print $2}'`

    LINEUP_FILE_XML=`get_latest_MSO_file ${MSO_FILE_PTN} ${COUNTRY} ${LINEUP_DIR}`
    LINEUP_FILE_ONLY=`basename $LINEUP_FILE_XML`
    LINEUP_FILE="${LINEUP_FILE_ONLY%.*}"

    SCHEDULE_FILE_XML=`get_latest_schedule_file $COUNTRY $SCHEDULE_DIR $SCHEDULE_DATE $OUTPUTDIR`
    SCHEDULE_FILE_ONLY=`basename $SCHEDULE_FILE_XML`
    SCHEDULE_FILE="${SCHEDULE_FILE_ONLY%.*}"

#    echo "schedule file: " $SCHEDULE_FILE_XML
#    echo "Lineup file: " $LINEUP_FILE_XML

    get_headend_info $LINEUP_FILE_XML > ${OUTPUTDIR}/${LINEUP_FILE}_Lineups.txt

    if [ ! -f ${OUTPUTDIR}/${SCHEDULE_FILE}_${COUNTRY}_channels.txt ]; then
      get_channel_list_in_schedule_file $SCHEDULE_FILE_XML > ${OUTPUTDIR}/${SCHEDULE_FILE}_${COUNTRY}_channels.txt
      get_program_title_lang_list $SCHEDULE_FILE_XML > ${OUTPUTDIR}/${SCHEDULE_FILE}_${COUNTRY}_program_title_lang.txt
      get_program_list_by_channel $SCHEDULE_FILE_XML ${OUTPUTDIR}/${SCHEDULE_FILE}_${COUNTRY}_program_title_lang.txt ${OUTPUTDIR}/${COUNTRY}
      get_channel_program_stat ${OUTPUTDIR}/${SCHEDULE_FILE}_${COUNTRY}_channels.txt ${COUNTRY} ${OUTPUTDIR}
    fi

    NUM_CH_IN_SCHEDULE=`cat ${OUTPUTDIR}/${SCHEDULE_FILE}_${COUNTRY}_channels.txt|wc -l`

    local LANG_STAT_IN_SCHEDULE=`get_lang_stat_in_schedule_file ${OUTPUTDIR}/${SCHEDULE_FILE}_${COUNTRY}_channels.txt`
    local LANG_STAT_IN_HEADEND=""

    while read LINE 
    do
      HEADEND_ID=`echo $LINE | awk -F "|" '{print $2}' | perl -pe 's/srid:\/\/bds.tv\///g'`
      DEVICE_TYPE_CODE=`echo $LINE | awk -F "|" '{print $3}' | perl -pe 's/ //g'`
      HEADEND_NAME=`echo $LINE | awk -F "|" '{print $4}'`
      MSO_NAME=`echo $LINE | awk -F "|" '{print $5}'`
      MSO_ID=`echo $LINE | awk -F "|" '{print $6}'`

      # echo "******** Headend id: $HEADEND_ID DEVICE_TYPE: $DEVICE_TYPE_CODE ******************"
      check_each_lineup $LINEUP_FILE_XML $HEADEND_ID $DEVICE_TYPE_CODE > ${OUTPUTDIR}/lineup_${LINEUP_FILE}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_ALL.txt
      compare_channels_lineup_schedule ${OUTPUTDIR}/${SCHEDULE_FILE}_${COUNTRY}_channels.txt $LINEUP_FILE_XML $HEADEND_ID $DEVICE_TYPE_CODE ${OUTPUTDIR} 

      NUM_CH_IN_HEADEND=`cat $OUTPUTDIR/lineup_${LINEUP_FILE}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_channels.txt | wc -l`
      NUM_CH_IN_HEADEND_WITH_SCHEDULE=`cat $OUTPUTDIR/lineup_${LINEUP_FILE}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_schedule.txt |wc -l`

      LANG_STAT_IN_HEADEND=`get_lang_stat_in_schedule_file $OUTPUTDIR/lineup_${LINEUP_FILE}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_schedule.txt`
      # echo "CO: " $COUNTRY "MSO: " $MSO_FILE_PTN "N-SCH: " $NUM_CH_IN_SCHEDULE $LANG_STAT_IN_SCHEDULE " N-HD: " $NUM_CH_IN_HEADEND " N-HD-S: " $NUM_CH_IN_HEADEND_WITH_SCHEDULE $LANG_STAT_IN_HEADEND
      echo "|"$COUNTRY"|"$NUM_CH_IN_SCHEDULE"|"$LANG_STAT_IN_SCHEDULE"|"$MSO_ID"|"$MSO_NAME"|"$HEADEND_ID"|"$DEVICE_TYPE_CODE"|"$HEADEND_NAME"|"$NUM_CH_IN_HEADEND"|"$NUM_CH_IN_HEADEND_WITH_SCHEDULE"|"$LANG_STAT_IN_HEADEND"|"
    done < ${OUTPUTDIR}/${LINEUP_FILE}_Lineups.txt

  done

}

main ${TARGET_FILE} ${SCHEDULE_DATE}
